package frontend.components.menu;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

import java.util.ArrayList;
import java.util.List;

public class TreeBasedMenu extends Panel {

	private List<? extends MenuItem> menuItems;
	private ListView<MenuItem> itemMenu;

	public TreeBasedMenu(String id, List<MenuItem> menuItems) {
		super(id);
		this.menuItems = filterVisibleItemsForLoggedInUser(menuItems);
		
		itemMenu = new ListView<MenuItem>("items", this.menuItems) {

			@Override
			protected void populateItem(ListItem<MenuItem> menuTab) {
				MenuItem menuItem = menuTab.getModelObject();
				Fragment menuItemFragment;
				if (menuItem.isLeaf()) {
					menuItemFragment = createLinkFragment("item", (LinkItem)menuItem);
				} else {
					menuItemFragment = createDropDownMenuFragment("item", (DropDownMenu) menuItem);
				}
				
				menuItemFragment.setOutputMarkupId(true);
				menuTab.add(menuItemFragment);
			}
		};
		add(itemMenu);
	}

	public ListView<MenuItem> getItemMenu() {
		return itemMenu;
	}

	public final Fragment createLinkFragment(String id, LinkItem linkItem) {
		Fragment fragment = new Fragment(id, "linkItem", this);
		fragment.add(createLink("menuLink", linkItem));
		return fragment;
	}
	
	public final Fragment createDropDownMenuFragment(String id, DropDownMenu dropDownMenuItem) {
		Fragment fragment = new Fragment(id, "linkItems", this);

        Link<Void> link = new Link<Void>("menuLink") {
			@Override
			public void onClick() {}
		};
		link.add(new Label("menuLinkText", new ResourceModel(
				dropDownMenuItem.getMenuTitle())));
		fragment.add(link);
		
		List<LinkItem> items = filterVisibleItemsForLoggedInUser(dropDownMenuItem.getItems());
		ListView<LinkItem> menuChildren = new ListView<LinkItem>("subItems", items) {
        	@Override
			protected void populateItem(ListItem<LinkItem> item) {
				Fragment menuChild = createLinkFragment("subItem", item.getModelObject());
				item.add(menuChild);
			}
		};
		fragment.add(menuChildren);

		return fragment;
	}

	private Link<Void> createLink(String id, final LinkItem linkItem) {
		Link<Void> link = new Link<Void>(id) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
                setResponsePage(linkItem.getLinkedPage());
			}
		};
		link.add(new Label("menuLinkText", new ResourceModel(linkItem.getItemTitle())));

		return link;
	}
	
	private <T extends MenuItem> List<T> filterVisibleItemsForLoggedInUser(List<T> allItems) {
		List<T> filteredItems = new ArrayList<T>();
		
		for (T menuItem : allItems) {
			if (menuItem.isVisibleForLoggedInUser()) {
				filteredItems.add(menuItem);
			}
		}
		return filteredItems;
	}
}